/*
 * @lc app=leetcode.cn id=111 lang=typescript
 *
 * [111] 二叉树的最小深度
 */

// @lc code=start

// Definition for a binary tree node.
class TreeNode {
  val: number
  left: TreeNode | null
  right: TreeNode | null
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val
    this.left = left === undefined ? null : left
    this.right = right === undefined ? null : right
  }
}

// 优化算法

// 递归
// function minDepth(root: TreeNode | null): number {
//   if (root === null) return 0
//   else if (root.left === null && root.right === null) return 1
//   else if (root.left === null) return minDepth(root.right) + 1
//   else if (root.right === null) return minDepth(root.left) + 1
//   else {
//     const leftDepth = minDepth(root.left)
//     const rightDepth = minDepth(root.right)
//     return Math.min(leftDepth, rightDepth) + 1
//   }
// }
// @lc code=end
